﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace GotLyrics.Controls
{
    public partial class ColumnHeaderCheckBox : Component
    {
        public ColumnHeaderCheckBox()
        {
            InitializeComponent();
        }

        public ColumnHeaderCheckBox(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        private ListView _listView;
        public ListView ListView
        {
            get { return this._listView; }
            set 
            { 
                this._listView = value;

                if (_listView != null)
                {
                    _listView.ItemChecked += new ItemCheckedEventHandler(ListViewItemChecked); 
                    _listView.HandleCreated += new EventHandler(ListViewHandleCreated);

                    InitCheckBox();
                }
            }
        }

        public IList<ListViewItem> GetCheckedItems()
        {
            List<ListViewItem> ret = new List<ListViewItem>();

            if (_listView != null)
            {
                foreach (ListViewItem lvItem in _listView.Items)
                {
                    if (lvItem.Checked)
                        ret.Add(lvItem);
                }
            }

            return ret;
        }

        void ListViewHandleCreated(object sender, EventArgs e)
        {
            InitCheckBox();
        }

        private void InitCheckBox()
        {
            if (_listView.Parent != null)
            {
                Point location = _listView.Location;
                
                if(Environment.OSVersion.Version.Major >= 6)
                    location.Offset(6, 6);
                else
                    location.Offset(8, 3);

                _checkBox.Location = location;
                _listView.Parent.Controls.Add(_checkBox);
                _checkBox.BringToFront();
            }
        }

        private bool _skipCheckChangedEvent = false;

        void ListViewItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (!_skipCheckChangedEvent)
            {
                _skipCheckChangedEvent = true;

                IList<ListViewItem> checkedItems = GetCheckedItems();

                if (checkedItems.Count == _listView.Items.Count)
                    _checkBox.CheckState = CheckState.Checked;
                else if (checkedItems.Count == 0)
                    _checkBox.CheckState = CheckState.Unchecked;
                else
                    _checkBox.CheckState = CheckState.Indeterminate;

                _skipCheckChangedEvent = false;
            }
        }

        private void CheckBoxCheckedChanged(object sender, EventArgs e)
        {
            if (!_skipCheckChangedEvent)
            {
                _skipCheckChangedEvent = true;

                foreach (ListViewItem lvItem in ListView.Items)
                {
                    lvItem.Checked = _checkBox.Checked;
                }

                _skipCheckChangedEvent = false;
            }
        }	
    }
}
